#include "dbnotescount.h"

DbNotesCount::DbNotesCount(QObject *parent) : QObject(parent)
{
    thread = new QThread();
    this->moveToThread(thread);
    thread->start();
}

void DbNotesCount::setDbCommName(const QString &dbCommName)
{
    m_dbCommName = dbCommName;
}

void DbNotesCount::setDbFileName(const QString &dbFileName)
{
    m_dbFileName = dbFileName;
}

void DbNotesCount::query(const QString sql)
{
    // 获取数据库
    if(!QSqlDatabase::contains(m_dbCommName))
    {
        m_Database = QSqlDatabase::addDatabase("QSQLITE", m_dbCommName);
        m_Database.setDatabaseName(m_dbFileName);
    }

    m_Database = QSqlDatabase::database(m_dbCommName);

    if(!m_Database.isOpen())
    {
        m_Database.open();
    }

    //执行查询

    QDateTime dtStart = QDateTime::currentDateTime();

    QSqlQuery query(m_Database);
    query.exec(sql);
    query.next();
    int count = query.value(0).toInt();

    QDateTime dtEnd = QDateTime::currentDateTime();
    double msec = dtStart.msecsTo(dtEnd);

    emit dbNotesCountQFinished(count, msec);
}


